<?php

declare(strict_types=1);

use Phinx\Migration\AbstractMigration;

final class File extends AbstractMigration
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */
    public function change(): void
    {
        $table = $this->table('file', [
            'collation' => 'utf8mb4_general_ci'
        ]);
        $table->addColumn('path', 'string', [
            'limit' => 255, 'null' => false, 'default' => '', 'comment' => '文件路径'
        ]);
        $table->addColumn('origin_name', 'string', [
            'limit' => 255, 'null' => false, 'default' => '', 'comment' => '原始文件名'
        ]);
        $table->addColumn('ext', 'string', [
            'limit' => 255, 'null' => false, 'default' => '', 'comment' => '文件后缀名'
        ]);
        $table->addColumn('size', 'integer', [
            'limit' => 1, 'null' => false, 'signed' => false, 'default' => '0', 'comment' => '文件大小'
        ]);
        $table->addColumn('file_hash', 'string', [
            'limit' => 255, 'null' => false, 'default' => '', 'comment' => '文件hash'
        ]);
        $table->addColumn('type', 'string', [
            'limit' => 64, 'null' => false, 'default' => 'public', 'comment' => '类型（disk）public/aliyunoss'
        ]);
        $table->addColumn('bucket', 'string', [
            'limit' => 255, 'null' => false, 'default' => '', 'comment' => '文件所属bucket'
        ]);
        $table->addColumn('endpoint', 'string', [
            'limit' => 255, 'null' => false, 'default' => '', 'comment' => '文件所属endpoint'
        ]);
        $table->addColumn('create_time', 'datetime', [
            'comment' => '创建时间'
        ]);
        $table->addIndex(['file_hash', 'type'], [
            'name' => 'idx_file_type_hash_unique',
            'unique' => true
        ]);
        $table->create();
    }
}
